<!-- 添加放箱协议 -->
<template>
  <div class="add-agreement-dialog">
    <el-dialog
      width="500px"
      class=""
      top="6vh"
      title="添加"
      :visible="$store.state.dialog.addAgreement"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-form :model="addForm" label-width="90px" size="mini" :rules="rules" ref="addForm">
            <el-row>
              <el-form-item label="议代码" prop="protocolCode">
                <el-input v-model="addForm.protocolCode" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="放箱公司" prop="putBoxCompany">
                <el-input v-model="addForm.putBoxCompany" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="地址">
                <el-input v-model="addForm.site" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="addForm.phone" auto-complete="off"></el-input>
              </el-form-item>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="loadShow" @click="sureAddBox('addForm')" size="mini">新 增</el-button>
        <el-button type="primary" size="mini" v-else :loading="true">加载中</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAgreement } from '../api/revocationSummary'
export default {
  data () {
    return {
      addForm: {},
      loadShow: true,
      rules: {
        protocolCode: [
          { required: true, message: '议代码', trigger: 'blur' },
          { min: 6, max: 6, message: '长度为6个字符的议代码', trigger: 'blur' }
        ],
        putBoxCompany: [
          { required: true, message: '请填写放箱公司', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 添加
    async addBoxagReement () {
      this.loadShow = false
      let that = this
      const res = await addAgreement(this.addForm)
      if (res.status === 200) {
        this.$parent.getBoxagReementmain()
        setTimeout(function () {
          that.loadShow = true
        }, 500)
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        this.$store.commit('hide_add_agreement_dialog')
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error',
          duration: 1000
        })
        this.loadShow = true
      }
    },
    // 确认添加
    sureAddBox (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addBoxagReement()
        } else {
          return false
        }
      })
    },
    close () {
      this.$store.commit('hide_add_agreement_dialog')
    }
  }
}
</script>

<style scoped lang="less">

</style>
